/**
 * Copyright 2019 [shujiang.yang], Inc. All rights reserved.
 * Email: 314938230@qq.com
 **/
define("pages/ccicwh/ccicwh_enter", [
        "../../plugins/easyui/jquery-easyui-1.5.1/jquery.easyui.min",
        "../../plugins/easyui/jquery-easyui-1.5.1/easyloader",
        "../../plugins/artTemplate/artTemplate",
        "../../common/tools",
        "../../common/componts",
        "../../common/kss",
        "./ccicwh_form",
        "../../pages/ccicwh/ccicwh_form.html.js"
    ],
    function (require) {
        var componts = require("../../common/componts");
        var kss = require("../../common/kss");
        var ccicwhDialog = require("./ccicwh_form");
        var businessTableId = "businessTableId";
        var id;

        var getQueryDataGrid = function () {
            return $('#' + businessTableId);
        };

        var initTable = function () {
            componts.initTable(businessTableId, {
                height: '615px',
                url: "/jbxx/list",
                fit: true,
                columns: [
                    {
                        field: 'id', title: '序号', formatter: function (value) {
                            return '<input type="radio" name="selectRadio1" value="' + value + '"/>';
                        }
                    }
                    , {
                        field: 'xm', title: '姓名', width: 20, sortable: 'true', formatter: function (value) {
                            return value ? '<span title="' + value + '">' + value + '</span>' : '';
                        }
                    }
                    , {
                        field: 'sfzh', title: '身份证号', width: 20, sortable: 'true', formatter: function (value) {
                            return value ? '<span title="' + value + '">' + value + '</span>' : '';
                        }
                    }
                    , {
                        field: 'tpsj', title: '逃跑时间', width: 20, sortable: 'true', formatter: function (value) {
                            return value ? '<span title="' + value + '">' + value + '</span>' : '';
                        }
                    }
                    , {
                        field: 'bdsj', title: '比对时间', width: 20, sortable: 'true', formatter: function (value) {
                            return value ? '<span title="' + value + '">' + value + '</span>' : '';
                        }
                    }
                    , {
                        field: 'sdm', title: '所代码', width: 20, sortable: 'true', formatter: function (value) {
                            return value ? '<span title="' + value + '">' + value + '</span>' : '';
                        }
                    }
                    , {
                        field: 'operate', title: '详细信息', align: 'center', width: 100,
                        formatter: function (value, row, index) {
                            var str = '<a href="#" data-target="#ckPhoto" value="' + index + '" name="opera" data-toggle="modal" class="easyui-linkbutton"><img src="../../img/xq.png"></a>';
                            return str;
                        }
                    }
                ],
                onLoadSuccess: function (data) {
                    if (data.total > 0) {
                        getQueryDataGrid().datagrid("selectRow", 0);
                        $("input[name='selectRadio1']")[0].checked = true;
                    }

                    $("a[name='opera']").each(function () {
                        $(this).on('click', function () {
                            var index = $(this).attr('value');
                            var jbxx = data.rows[index];
                            kss.initJbxxxx(jbxx.gcbh, jbxx.rybh, jbxx.xm + "--详细信息");
                        });
                    });
                },
                //单击行选中取消事件
                onClickRow: function (rowIndex, rowData) {
                    if (id != rowData.id) {
                        $("input[name='selectRadio1']")[rowIndex].checked = true;
                        getQueryDataGrid().datagrid("selectRow", rowIndex);
                        id = rowData.id;
                    } else if (id == rowData.id) {
                        getQueryDataGrid().datagrid("unselectRow", rowIndex);
                        $("input[name='selectRadio1']")[rowIndex].checked = false;
                        id = "";
                    }
                    //加入列表中
                    kss.zjryClick(rowData);
                },
                queryParams: {
                    "processDefinitionKey": componts.getUrlParam('processDefinitionKey'),
                    "taskDefinitionKey": componts.getUrlParam('taskDefinitionKey'),
                    "pageSize": function () {
                        return getQueryDataGrid().datagrid("getPager").pagination("options").pageSize;
                    },
                    "pageIndex": function () {
                        return getQueryDataGrid().datagrid("getPager").pagination("options").pageNumber;
                    }
                }
            })
            //页面高度自适应
            getQueryDataGrid().datagrid('resize', {
                height: ($(window).height() - 140)
            });
        }
        /**
         * 数据列表刷新加载
         */
        var loadDataGrid = function () {
            var formData = $("#yws").serializeObject();
            var queryParams = $.extend({}, formData, {
                "state": "R8",
                "processDefinitionKey": componts.getUrlParam('processDefinitionKey'),
                "taskDefinitionKey": componts.getUrlParam('taskDefinitionKey'),
                "pageSize": function () {
                    return getQueryDataGrid().datagrid("getPager").pagination("options").pageSize;
                },
                "pageIndex": function () {
                    return getQueryDataGrid().datagrid("getPager").pagination("options").pageNumber;
                }
            });
            getQueryDataGrid().datagrid('clearSelections');
            getQueryDataGrid().datagrid('load', queryParams);
        };

        //绑定事件
        var bindAction = function () {
            //查询事件
            $("#ccicwhiii").on("click", function () {
                loadDataGrid();
            });

            $(".awd-add-btn").on('click', function () {
                var selectedPerson = getQueryDataGrid().datagrid('getSelected');
                ccicwhDialog.show(selectedPerson, loadDataGrid);
            });
        };

        var init = function () {
            initTable();
            bindAction();
            componts.initSelectPage(undefined);
            componts.initSelect(undefined, undefined, undefined);
        };
        init();
    });